package com.wen.controller;

import com.wen.common.R;
import com.wen.pojo.PurchaseOrder;
import com.wen.pojo.SupplierProduct;
import com.wen.pojo.dto.PurchaseOrderDto;
import com.wen.service.PurchaseOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/purchase")
public class PurchaseOrderController {
    @Autowired
    private PurchaseOrderService service;


    @GetMapping("/PurByIds/{id}")
    public R PurByIds(@RequestBody @PathVariable Long id){
        List<PurchaseOrder> purchaseOrder = service.PurByIds(id);
        return R.ok().data("purchaseOrder",purchaseOrder);

    }
    @PutMapping("PurUp/{id}")
    public R PurUp(@PathVariable String id){
        boolean flag = service.purUp(id);
        if (flag)
            return R.ok().message("修改成功");
        return R.error().message("修改失败");
    }

    @Autowired
    private PurchaseOrderDetailsService service1;

    @Autowired
    private StockService service2;

    @PostMapping("sel/{pageNum}/{pageSize}")
    public R sel(@RequestBody PurchaseOrderDto dto, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {

        System.out.println(dto);
        Page<PurchaseOrders> sel = service.sel(dto, pageNum, pageSize);

        return R.ok().data("records", sel.getRecords()).
                data("total", sel.getTotal()).
                data("current", sel.getCurrent()).
                data("size", sel.getSize());
    }

    @GetMapping("selById/{id}")
    public R selById(@PathVariable String id) {

        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("purchase_id", id);

        // 根据id查询采购表
        PurchaseOrder byId = service.getById(id);
        // 根据采购表的id查询采购明细表
        List<PurchaseOrderDetailsDto> purchaseOrderDetailsDtos = service1.selById(id);
        System.out.println(purchaseOrderDetailsDtos);
        return R.ok().data("byId", byId).data("purchaseOrderDetails", purchaseOrderDetailsDtos);
    }

    @PostMapping("/up/{checkTag}/{id}")
    public R up(@PathVariable String checkTag, @PathVariable String id) {
        System.out.println(checkTag);
        String aa = "";
        if (checkTag.equals("1") || checkTag == "1") {
            aa = "s001-3";
        } else {
            aa = "s001-2";
        }
        System.out.println(aa);
        boolean b = service.up(aa, id);
        System.out.println(b);
        if (b)
            return R.ok().message("修改成功");
        return R.ok().message("修改失败");
    }

    /**
     * 采购单查询（采购订单管理）
     * 根据采购单审核状态（check_tag）查询
     */
    @PostMapping("selCheckTag/{pageNum}/{pageSize}")
    public R selCheckTag(@RequestBody PurchaseOrderDto dto, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {

        Page<PurchaseOrders> purchaseOrdersPage = service.selCheckTag(dto, pageNum, pageSize);

        return R.ok().data("records", purchaseOrdersPage.getRecords()).
                data("total", purchaseOrdersPage.getTotal()).
                data("current", purchaseOrdersPage.getCurrent()).
                data("size", purchaseOrdersPage.getSize());
    }

    /**
     * 采购单入库查询（采购订单管理）
     *
     * @param id
     * @return
     */
    @PostMapping("selRK/{id}")
    public R selRK(@PathVariable String id) {
        System.out.println("PurchaseOrder进来了");
        PurchaseOrder byId = service.getById(id);

        List<PurchaseOrderDetailsSP> purchaseOrderDetailsSPS = service1.selPurchaseOrderDetailsSP(id);

        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("stock_type", "t001-1");
        List<Stock> stocks = service2.listByMap(columnMap);

        return R.ok().data("byId", byId).
                data("purchaseOrderDetailsSPS", purchaseOrderDetailsSPS).
                data("stocks", stocks);
    }

    /**
     * 采购单申请
     *
     * @return
     */
    @PostMapping("add/{username}/{js}")
    public R add(@PathVariable String username, @PathVariable int js, @RequestBody List<SupplierANDProduct> dto) {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        PurchaseOrderDetails purchaseOrderDetails = null;
        System.out.println(username);
        System.out.println(dto);
        System.out.println(js);
        float aa = 0;
        float sl = 0;
        purchaseOrder.setPurchaseOrderNumber("555555");

        for (SupplierANDProduct s : dto) {
            aa += js * s.getPrice();
            sl += js;
            purchaseOrder.setSupplierId(Long.valueOf(s.getSupplierId()));
            purchaseOrder.setTotalAmount((long) sl);
            purchaseOrder.setTotalPrice(aa);
            purchaseOrder.setRegister(username);
            purchaseOrder.setRegisterTime(new Date());
            purchaseOrder.setCheckTag("s001-1");
            purchaseOrder.setPayMethod("p002-1");
            purchaseOrder.setGatherTag("k001-1");
            purchaseOrder.setConveyTag("p001-1");
        }

        boolean save = service.save(purchaseOrder);
        System.out.println("采购=" + save);
        if (save) {
            for (SupplierANDProduct s : dto) {
                aa += js * s.getPrice();
                purchaseOrderDetails = new PurchaseOrderRKDto();
                purchaseOrderDetails.setPurchaseId(purchaseOrder.getPurchaseId());
                purchaseOrderDetails.setProductId(Long.valueOf(s.getId()));
                purchaseOrderDetails.setCount(s.getCount());
                purchaseOrderDetails.setPrice(s.getPrice());
                purchaseOrderDetails.setSubtotal(aa);
                purchaseOrderDetails.setGatherTag("k001-1");
                boolean save1 = service1.save(purchaseOrderDetails);
                System.out.println("采购明细=" + save1);
            }
        }


        return R.ok().message("添加成功").data("total", 1);
    }

}
